﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SMS
{
    public partial class fDoimatkhau : Form
    {
        fLogin main;
        public fDoimatkhau(fLogin main1)
        {
            InitializeComponent();
            main = main1;
        }



        private void button2_Click(object sender, EventArgs e)
        {
            this.Visible = false;

        }

        private void bntThoat_Click(object sender, EventArgs e)
        {
            this.Visible = false;
        }

        private void fDoimatkhau_Load(object sender, EventArgs e)
        {
            txtTaiKhoan.Text = main.textBox1.Text.ToString();
        }

        private void bntSave_Click(object sender, EventArgs e)
        {

            SqlConnection conn;
            SqlCommand command;
            SqlCommand command2;
            conn = new SqlConnection(main.ConnectDB);
            command = new SqlCommand();
            command.Connection = conn;
            command.CommandText = "select * from taikhoan where tentk=@tentk and matkhaumd=@mk";
            command2 = new SqlCommand();
            command2.Connection = conn;
            command2.CommandText = "update taikhoan set matkhaumd=@mkmoi where tentk=@tentk COLLATE SQL_Latin1_General_CP1_CS_AS";

            try
            {
                conn.Open();
                command.Parameters.Add(new SqlParameter("@tentk", txtTaiKhoan.Text.ToString()));
                command.Parameters.Add(new SqlParameter("@mk", txtMatKhauCu.Text.ToString()));
                command2.Parameters.Add(new SqlParameter("@tentk", txtTaiKhoan.Text.ToString()));
                command2.Parameters.Add(new SqlParameter("@mkmoi", txtMatKhauMoi.Text.ToString()));
                SqlDataReader reader = command.ExecuteReader();
                if (txtMatKhauCu.Text == null || txtMatKhauCu.Text == "")
                {
                    MessageBox.Show("Nhập mật khẩu cũ!");
                }
                else
                {
                    if (txtMatKhauMoi.Text == null || txtMatKhauMoi.Text == "")
                    {
                        MessageBox.Show("Nhập mật khẩu mới!");
                    }
                    else
                    {
                        if (txtMatKhauMoi1.Text == null || txtMatKhauMoi1.Text == "")
                        {
                            MessageBox.Show("Nhập lại mật khẩu mới!");
                        }
                        else
                        {
                            if (txtMatKhauMoi.Text.ToString() != txtMatKhauMoi1.Text.ToString())
                            {

                                MessageBox.Show("Mật khẩu mới không trùng khớp!");
                            }
                            else
                            {
                                if (reader.Read())
                                {
                                    reader.Close();
                                    command2.ExecuteNonQuery();
                                    MessageBox.Show("Đổi mật khẩu thành công!");
                                    this.Visible = false;

                                }
                                else
                                {
                                    MessageBox.Show("Nhập sai mật khẩu cũ!");
                                }
                            }

                        }
                    }
                }

            }
            catch (SqlException ex)
            {
                string error = ex.Message;
            }
            finally
            {
                command.Dispose();
                command2.Dispose();
                conn.Close();
            }


        }
    }
}
